package com.godofgamblers.hotelmanagementsystem.mapper;

import com.godofgamblers.hotelmanagementsystem.dto.OfflinePriceDto;
import com.godofgamblers.hotelmanagementsystem.dto.OrderItemDTO;
import com.godofgamblers.hotelmanagementsystem.pojo.Comment;
import com.godofgamblers.hotelmanagementsystem.pojo.Order_item;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wzx
 * @date 2019/10/12 - 11:55
 * 订单项
 */
@Repository
public interface Order_ItemMapper {
    //
    //点击创建订单时候，往订单表插入一条该笔订单信息，循环遍历购物车依次插入订单项
    //先往订单表插，插了之后找到该笔订单的order_id，在插入订单项表中
    @Insert("insert into order_item(order_id,room_type_id,plan_stay_time,plan_leave_time,order_item_state) values(#{order_id},#{room_type_id},#{plan_stay_time},#{plan_leave_time},#{order_item_state})")
    public void insertOrderItem(Order_item order_item);


    //用户支付成功之后，将订单项状态从未支付（0）变为未使用（1） 通过订单号找到order_id，然后将拥有该号码的订单项状态都变为未使用
    @Update("update order_item set order_item_state = 1 where order_id = #{order_id} and flag =1")
    public void updateOrderItemState(Integer order_id);

    //查询指定order_id下的所有订单项
    @Select("select * from order_item where order_id = #{order_id} and flag = 1")
    public List<Order_item> findorderItemByOrderId(Integer order_id);

    //传入订单项id，将对应的状态改为4--->用户取消
    @Update("update order_item set order_item_state = 4 where order_id = #{order_id} and flag = 1")
    public void updateOrderItemStates(Integer order_id);



    /**
     * 线下修改订单项状态
     * @param order_itemPojo 订单项pojo类
     * @return int 影响行数
     */
    @Update("update order_item set order_item_state=#{order_item_state} where order_item_id=#{order_item_id}")
    public int updateState(Order_item order_itemPojo);

    /**
     * 查询订单id
     * @param order_item_id 订单项id
     * @return int 订单id
     */
    @Select("select order_id from order_item where order_item_id=#{order_item_id} and flag=1")
    public int findOrderId(Integer order_item_id);

    /**
     *  通过房间类型id和入住起止时间查找已使用房间数量
     * @param offlinePriceDto 传入房间类型、计划入住时间、计划退房时间
     * @return int 已使用房间数量
     */
    @Select("SELECT count(order_item_id)\n" +
            "FROM order_item\n" +
            "WHERE  ((DATE_FORMAT(plan_stay_time,'%Y-%m-%d') <= #{startTime} AND #{startTime}<DATE_FORMAT(plan_leave_time,'%Y-%m-%d'))\n" +
            "\n" +
            "OR (DATE_FORMAT(plan_stay_time,'%Y-%m-%d') < #{endTime} AND #{endTime}<= DATE_FORMAT(plan_leave_time,'%Y-%m-%d')))\n" +
            "\n" +
            "AND flag = 1\n" +
            "\n" +
            "AND room_type_id=#{room_type_id}\n" +
            "\n" +
            "AND (order_item_state = 1 OR order_item_state = 2 or order_item_state = 3)")
    public int offlineFindUsedRoomNumByRoomTypeId(OfflinePriceDto offlinePriceDto);

    /**
     * 线下插入订单项
     * @param order_item  要添加的订单项信息
     * @return  int 受影响行数
     */
    @Insert("insert into order_item(order_item_num,order_id,room_type_id,plan_stay_time,plan_leave_time,order_item_state) values(#{order_item_num},#{order_id},#{room_type_id},#{plan_stay_time},#{plan_leave_time},#{order_item_state})")
    public int offlineAddOrderItem(Order_item order_item);

    /**
     * 线下查询订单项id
     * @param order_item_num  订单项编号
     * @return Integer 订单项id
     */
    @Select("select order_item_id from order_item where order_item_num=#{order_item_num} and flag=1")
   public Integer offlineFindOrderItemIdByOrderItemNum(String order_item_num);

    /**
     * 查询订单项信息
     * @param order_item_id
     * @return
     */
    @Select("select order_item_state,plan_stay_time,plan_leave_time,room_type_id from order_item where order_item_id=#{order_item_id} and flag=1")
   public Order_item findOrderItemInfoByOrderitemId(int order_item_id);

    /**
     * 根据订单项id查询订单项状态
     * @param order_item_id 订单项id
     * @return  订单项状态
     */
    @Select("select order_item_state from order_item where order_item_id=#{order_item_id} and flag=1")
    Integer findOrderItemStateByOrderItemId(Integer order_item_id);
    //查询当前用户的已完成，未评价订单项（查询：order_item_id,room_type_id,order_time）
    @Select("SELECT order_item_id,room_type_id,order_time FROM `order` o,order_item oi \n" +
            "WHERE user_id = #{user_id} \n" +
            "AND o.order_id = oi.order_id \n" +
            "AND oi.order_item_common = 0 \n" +
            "AND oi.order_item_state = 5")
    public List<OrderItemDTO> findDto(Integer user_id);


    //将对应的订单项改为已经评价
    @Update("update order_item set order_item_common = 1 where order_item_id = #{order_item_id} and flag = 1")
    public void updateOrderCommonState(Integer order_item_id);


    //插入一条评论
    @Insert("insert into type_comment(comment_context,room_type_id,user_id,comment_time) values(#{comment_context},#{room_type_id},#{user_id},#{comment_time})")
    public void insertCommon(Comment comment);
}
